﻿using Nasty.Core.LoadParams;
using Nasty.Core.Repository;
using Nasty.Core.SuperExtension;
using Nasty.FlowModule.Areas.FlowTest.Model;
using Nasty.FlowModule.Areas.FlowTest.Params;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Nasty.FlowModule.FlowTest
{
    public interface IFlowTestRepository : IRepository<FlowTest>
    {
        public FlowTest GetFlowTest(string id);

        public ResultData<FlowTest> SaveFlowTest(FlowTestModel model);

        public ResultData<string> DeleteFlowTests(List<string> ids);

        public PageData<FlowTest> GetFlowTestPage(GetFlowTestPageParams @params);

        public List<FlowTest> GetFlowTests(GetFlowTestsParams @params);
    }

    public class FlowTestRepository : SqlRepository<FlowTest>, IFlowTestRepository
    {
        public FlowTestRepository(SqlSugarClient db) : base(db)
        {
           // db.CodeFirst.InitTables(typeof(FlowTest));
        }

        public ResultData<string> DeleteFlowTests(List<string> ids)
        {
            var result = new ResultData<string>();
            try
            {
                var FlowTests = Db.Queryable<FlowTest>().In(ids).ToList();
                foreach (var FlowTest in FlowTests)
                {
                    Db.Delete(FlowTest);
                }

                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

        public FlowTest GetFlowTest(string id)
        {
            return this.Db.Queryable<FlowTest>().IncludesAllFirstLayer().InSingle(id);
        }

        public PageData<FlowTest> GetFlowTestPage(GetFlowTestPageParams @params)
        {
            int totalPage = 0;
            int total = 0;
            var pageData = new PageData<FlowTest>();

            var _SQLExpress = Db.Queryable<FlowTest>();

            if (!string.IsNullOrEmpty(@params.Name)) _SQLExpress.Where((t) => t.Name.Contains(@params.Name));

            _SQLExpress = _SQLExpress.OrderBy((t) => t.CreateTime, OrderByType.Desc);

            var data = _SQLExpress.ToPageList(@params.Current, @params.PageSize, ref total, ref totalPage);

            pageData.Total = total;
            pageData.TotalPage = totalPage;
            pageData.Data = data;

            pageData.Current = @params.Current;
            pageData.PageSize = @params.PageSize;
            return pageData;
        }

        public List<FlowTest> GetFlowTests(GetFlowTestsParams @params)
        {
            var _SQLExpress = Db.Queryable<FlowTest>();
            if (!string.IsNullOrEmpty(@params.Name)) _SQLExpress.Where((t) => t.Name.Contains(@params.Name));

            return _SQLExpress.ToList();
        }

        public ResultData<FlowTest> SaveFlowTest(FlowTestModel model)
        {
            var result = new ResultData<FlowTest>();
            try
            {
                var data = Db.Save<FlowTest>(model);
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }
    }
}
